//
//  MoreBrandTableViewController.m
//  pmm
//
//  Created by WTFSSD on 2017/9/7.
//  Copyright © 2017年 WTFSSD. All rights reserved.
//

#import "MoreBrandTableViewController.h"


#import <WEPopover/WEPopoverController.h>
@interface MoreBrandTableViewController ()

/**索引*/
@property(nonatomic,strong) NSArray * indexs_array;


/**数据u*/
@property(nonatomic,strong)NSMutableDictionary * mut_dic_datas;

/**选中分类*/
@property(nonatomic,strong)NSMutableArray *array_selecteds;



@end

@implementation MoreBrandTableViewController



/**lazy */
-(NSMutableArray*)array_selecteds{
    if (!_array_selecteds) {
        _array_selecteds = [NSMutableArray array];
        [_array_selecteds addObjectsFromArray:self.selectedCategory];
    }
    return _array_selecteds;
    
}
-(NSMutableDictionary *)mut_dic_datas{
    if (!_mut_dic_datas) {
        _mut_dic_datas = [NSMutableDictionary dictionary];
    }
    return _mut_dic_datas;
}

+(instancetype)controllerWith:(UITableViewStyle)tableViewStyle{

    return [super controllerWith:UITableViewStyleGrouped];

}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.preferredContentSize = CGSizeMake(SCREEN_WIDTH-20, SCREENH_HEIGHT);
    UIBarButtonItem * back = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"icon_nav_arrow_left"] style:UIBarButtonItemStylePlain target:self action:@selector(back)];
    self.navigationItem.leftBarButtonItem = back;
    self.navigationItem.title = @"品牌";
    self.navigationController.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName:HexColor(0x333333)};
    
    UIBarButtonItem * ok = [[UIBarButtonItem alloc] initWithTitle:@"确定" style:UIBarButtonItemStylePlain target:self action:@selector(ok)];
    ok.tintColor = HexColor(0x898C8F);
    self.navigationItem.rightBarButtonItem = ok;
    self.navigationController.navigationBar.tintColor = UIColor.whiteColor;
    self.navigationController.navigationBar.backgroundColor = UIColor.whiteColor;
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"UITableViewCell"];
    self.tableView.sectionIndexColor = [UIColor blackColor];
    self.tableView.sectionFooterHeight = 0.1f;
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.tableView reloadData];
}
-(void)back{
    [[WEPopoverController visiblePopovers].lastObject dismissPopoverAnimated:YES];
}

-(void)ok{
    WeakSelf(weakself);
    [[WEPopoverController visiblePopovers].lastObject dismissPopoverAnimated:YES completion:^{
        if (weakself.didSelectedCallBack) {
            weakself.didSelectedCallBack(weakself.array_selecteds);
        }
    }];
}

-(void)setDataSource:(NSArray *)dataSource{
    _dataSource = dataSource;
    WeakSelf(weakself);

    self.indexs_array =  [[dataSource map:^id(id obj, int index) {
        NSString * label = obj[@"label"];
        NSString * key = [label firstCharactor];
        
        NSMutableArray * arr = [NSMutableArray array];
        if (weakself.mut_dic_datas[key]) {
            [arr addObjectsFromArray:weakself.mut_dic_datas[key]];
            [arr addObject:obj];
        }else{
            [arr addObject:obj];
        }
        weakself.mut_dic_datas[key] = arr;
        
        return key;
    }] distinct] ;
    
    [self.tableView reloadData];
}


-(void)setSelectedCategory:(NSArray *)selectedCategory{
    [self.array_selecteds removeAllObjects];
    [self.array_selecteds addObjectsFromArray:selectedCategory];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.indexs_array?self.indexs_array.count:0;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    NSString * key = self.indexs_array[section];
    NSArray * arr = self.mut_dic_datas[key];
    return (key!=nil&&arr!=nil)?arr.count:0;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString * key = self.indexs_array[indexPath.section];
    NSArray * arr = self.mut_dic_datas[key];
    NSDictionary * obj =  arr[indexPath.row];
    
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCell"];
    cell.textLabel.text = obj[@"label"];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    if ([self.array_selecteds indexOfObject:obj]!=NSNotFound) {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
    }else{
        cell.accessoryType = UITableViewCellAccessoryNone;
    }
    return cell;
}

-(NSString*)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    return self.indexs_array?self.indexs_array[section]:@"";
}


-(NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView{
    return self.indexs_array?self.indexs_array:@[];
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    id obj =  self.mut_dic_datas[self.indexs_array[indexPath.section]][indexPath.row];
    if ([self.array_selecteds indexOfObject:obj]== NSNotFound) {
        [self.array_selecteds addObject:obj];
    }else{
        [self.array_selecteds removeObject:obj];
    }


    
    [tableView reloadData];
    
}
@end
